package com.javaee.xiangyang.service.impl;

import com.javaee.xiangyang.common.ServiceResultEnum;
import com.javaee.xiangyang.dto.GoodsComment;
import com.javaee.xiangyang.dto.GoodsDetails;
import com.javaee.xiangyang.entity.Classify;
import com.javaee.xiangyang.entity.Comment;
import com.javaee.xiangyang.entity.Goods;
import com.javaee.xiangyang.mapper.ClassifyMapper;
import com.javaee.xiangyang.mapper.CommentMapper;
import com.javaee.xiangyang.mapper.GoodsMapper;
import com.javaee.xiangyang.service.GoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    CommentMapper commentMapper;

    @Resource
    GoodsMapper goodsMapper;

    @Resource
    ClassifyMapper classifyMapper;

    /**
     * 新增商品
     */
    @Override
    @Transactional
    public ServiceResultEnum addGoods(Goods goods) {
        goodsMapper.addGoods(goods);
        return ServiceResultEnum.SUCCESS;
    }

    /**
     * 获取所有商品信息
     */
    @Override
    public List<Goods> findAllGoods() {
        return goodsMapper.findAllGoods();
    }

    /**
     * 根据类别查找对应分类所包含的商品
     */
    @Override
    public List<Goods> findAllGoodsByClassifyId(Classify classify) {
        return goodsMapper.findAllGoodsByClassifyId(classify);
    }

    /**
     * 获取商品所有信息
     */
    @Override
    public GoodsDetails findGoodsDetails(Goods goods) {
        //获取对应商品信息
        Goods goodsByGoodsId = goodsMapper.findGoodsByGoodsId(goods);
        //获取对应评论信息
        List<GoodsComment> allCommentByGoodsId = commentMapper.findAllGoodsCommentByGoodsId(goods);
        if (goodsByGoodsId != null) {
            //获取的商品信息不为空
            GoodsDetails goodsDetails = new GoodsDetails();
            goodsDetails.setGoods(goodsByGoodsId);
            goodsDetails.setGoodsComments(allCommentByGoodsId);
            return goodsDetails;
        }
        //商品不存在
        return null;
    }

    /**
     * 根据生成订单最多的商品排序
     */
    @Override
    public List<Goods> findGoodsOrderBy() {
        List<Integer> goodsByNumber = goodsMapper.findGoodsByNumber();
        //循环查找然后封装
        List<Goods> goodsList = new ArrayList<>();
        for (int i : goodsByNumber) {
            Goods goodsByGoodsId = goodsMapper.findGoodsByGoodsId(new Goods(i));
            goodsList.add(goodsByGoodsId);
        }
        return goodsList;
    }

    /**
     * 获取所有商品信息(包括评论)
     */
    @Override
    public List<GoodsDetails> findAllGoodsDetails() {
        //模糊查询查找到的商品集合
        List<Goods> goodsBySomething = goodsMapper.findAllGoods();
        //创建一个DTO对象实体
        return getGoodsDetails(goodsBySomething);
    }


    /**
     * 模糊查询
     */
    @Override
    public List<GoodsDetails> findGoodsBySomething(String string, Classify classify) {
        //模糊查询查找到的商品集合
        List<Goods> goodsBySomething = goodsMapper.findGoodsBySomething(string, classify);
        //创建一个DTO对象实体
        return getGoodsDetails(goodsBySomething);
    }

    /**
     * 根据商品存储量查找
     */
    @Override
    public List<Goods> findGoodsByInventory(Goods goods) {
        return goodsMapper.findGoodsByInventory(goods);
    }

    /**
     * 更改商品库存量
     */
    @Override
    @Transactional
    public ServiceResultEnum updateInventoryByGoodsId(Goods goods, boolean type) {
        if (goodsMapper.findGoodsByGoodsId(goods) != null) {
            //商品存在
            goodsMapper.updateInventoryByGoodsId(goods, type);
            return ServiceResultEnum.SUCCESS;
        }
        //商品不存在
        return ServiceResultEnum.GOODS_NOT_EXIST;
    }

    /**
     * 更改商品信息
     */
    @Override
    @Transactional
    public ServiceResultEnum updateCommentByCommentId(Goods goods) {
        if (goodsMapper.findGoodsByGoodsId(goods) != null) {
            //商品存在
            if (goods.getGoodsImg() == null) {
                //没有修改商品图片
                goods.setGoodsImg(goodsMapper.findGoodsByGoodsId(goods).getGoodsImg());
            }
            goodsMapper.updateByGoodsId(goods);
            return ServiceResultEnum.SUCCESS;
        }
        //商品不存在
        return ServiceResultEnum.GOODS_NOT_EXIST;
    }

    /*===================================Classify======================================*/

    /**
     * 新增商品分类
     * 暂时不实现
     */
    @Override
    @Transactional
    public ServiceResultEnum addClassifyMapper(Classify classify) {
        classifyMapper.addClassifyMapper(classify);
        return ServiceResultEnum.SUCCESS;
    }

    /**
     * 用户手机主页展示商品分类(没有旗下商品信息)
     */
    @Override
    public List<Classify> findAllClassify() {
        return classifyMapper.findAllClassify();
    }

    /**
     * 用户手机主页展示商品分类以及所有商品信息
     */
    @Override
    public List<Classify> findAllClassifyAndGoods() {
        return classifyMapper.findAllClassifyAndGoods();
    }

    /**
     * 用户主页展示对应商品分类的商品信息
     */
    @Override
    public Classify findClassifyById(Classify classify) {
        return classifyMapper.findClassifyById(classify);
    }

    /**
     * 管理员修改对应商品分类的备注
     */
    @Override
    @Transactional
    public ServiceResultEnum updateDescribeByClassifyId(Classify classify) {
        if (classifyMapper.findClassifyById(classify) != null) {
            //此分类存在
            classifyMapper.updateDescribeByClassifyId(classify);
            return ServiceResultEnum.SUCCESS;
        }
        return ServiceResultEnum.CLASSIFY_NULL;
    }

    /**
     * 管理员删除对应商品分类
     */
    @Override
    @Transactional
    public ServiceResultEnum deleteClassifyByClassifyId(Classify classify) {
        if (classifyMapper.findClassifyById(classify) != null) {
            //此分类存在
            classifyMapper.deleteClassifyByClassifyId(classify);
            return ServiceResultEnum.SUCCESS;
        }
        //分类不存在
        return ServiceResultEnum.CLASSIFY_NULL;
    }

    /*========================================提取公共代码===========================================*/

    /**
     * 根据传入的Goods集合的值  返回对应值的所有评论
     */
    private List<GoodsDetails> getGoodsDetails(List<Goods> goodsBySomething) {
        List<GoodsDetails> goodsDetails = new ArrayList<>();
        for (Goods goods : goodsBySomething) {
            //对应查找到的评论集合
            List<GoodsComment> allCommentByGoodsId = commentMapper.findAllGoodsCommentByGoodsId(goods);
            GoodsDetails goodsDetails1 = new GoodsDetails();
            goodsDetails1.setGoods(goods);
            goodsDetails1.setGoodsComments(allCommentByGoodsId);
            goodsDetails.add(goodsDetails1);
        }
        return goodsDetails;
    }
}
